package cn.edu.nyist.controller;

import cn.edu.nyist.entity.CommonUser;
import cn.edu.nyist.entity.Region;
import cn.edu.nyist.service.CommonUserService;
import cn.edu.nyist.service.RegionService;
import cn.edu.nyist.util.AjaxResult;
import cn.edu.nyist.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("yju")
public class ClientController {
    private static Logger log = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private AjaxResult ajaxResult;

    @Autowired
    private CommonUserService commonUserService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private RegionService regionService;

    /**
     * 跳转首页
     * @return
     */
    @GetMapping("/index")
    public String index(){

        return "yju/client/index";
    }

    /**
     * 跳转登录界面
     * @return
     */
    @GetMapping("/login")
    public String login(){
        return "yju/client/login";
    }

    /**
     * 跳转注册界面
     * @return
     */
    @GetMapping("/register")
    public String register(){
        return "yju/client/register";
    }
    /*
     * 跳转到卖房信息
     *
     * */
    @GetMapping("/sellHouseIndex")
    public String sellHouseIndex(){
        return "yju/client/sellHouseIndex";
    }

    @GetMapping("/rentHouseIndex")
    public String rentHouseIndex(){
        return "yju/client/rentHouseIndex";
    }

    @GetMapping("/showRentHouseInfo")
    public String showRentHouseInfo(){
        return "yju/client/showRentHouseInfo";
    }

    @GetMapping("/showSellHouseInfo")
    public String showSellHouseInfo(){
        return "yju/client/showSellHouseInfo";
    }


    @GetMapping("/forgetPassword")
    public String forgetPassword(){
        return "yju/client/forgetPassword";
    }

    @GetMapping("/resetPassword")
    public String resetPassword(){
        return "yju/client/resetPassword";
    }

    @RequestMapping("/indexSearch")
    @ResponseBody
    public AjaxResult indexSearch(String title,String houseSelect){
        log.info("title:"+title);
        log.info("house_type:"+houseSelect);
        ajaxResult.ajaxTrue("跳转");
        return ajaxResult;
    }

    @RequestMapping("/personInfo")
    public String personInfo(HttpSession session){
        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        return "yju/client/personInfo";
    }

    @RequestMapping("/passwordReset")
    public String passwordReset(HttpSession session){
        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        return "yju/client/passwordReset";
    }

    @RequestMapping("/publishRentHouseInfo")
    public String publishRentHouseInfo(HttpSession session){
        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        List<Region> regions = regionService.queryAllRegion();
        session.setAttribute(Const.REGIONLIST,regions);
        session.setAttribute(Const.USERADDRENTHOUSE,"yes");
        return "yju/client/publishRentHouseInfo";
    }

    @RequestMapping("/publishSellHouseInfo")
    public String publishSellHouseInfo(HttpSession session){
        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        List<Region> regions = regionService.queryAllRegion();
        session.setAttribute(Const.REGIONLIST,regions);
        session.setAttribute(Const.USERADDSELLHOUSE,"yes");
        return "yju/client/publishSellHouseInfo";
    }

    @RequestMapping("/rentHouseInfoList")
    public String rentHouseInfoList(HttpSession session){

        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        List<Region> regions = regionService.queryAllRegion();
        session.setAttribute(Const.REGIONLIST,regions);
        session.setAttribute(Const.USERRENTHOUSEINFOLIST,"yes");
        return "yju/client/rentHouseInfoList";
    }

    @RequestMapping("/sellHouseInfoList")
    public String sellHouseInfoList(HttpSession session){

        CommonUser commonUser = (CommonUser)session.getAttribute(Const.COMMONUSER);
        if( commonUser ==null ){
            return "yju/client/login";
        }
        List<Region> regions = regionService.queryAllRegion();
        session.setAttribute(Const.REGIONLIST,regions);
        session.setAttribute(Const.USERSELLHOUSEINFOLIST,"yes");
        return "yju/client/sellHouseInfoList";
    }


    @RequestMapping("/logout")
    public String  logout(HttpSession session){
        session.invalidate();
        return "yju/client/index";
    }

}
